package com.yuandian.middle.platform.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.middle_platform.po.AlarmDetailPojo;
import com.yuandian.api.middle_platform.po.OverViewParam;
import com.yuandian.api.middle_platform.vo.OrgOverviewDetailVo;
import com.yuandian.api.middle_platform.vo.OrgTypeAbnormalAbilityVo;
import com.yuandian.api.middle_platform.vo.OrgTypeAlarmVo;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.middle.platform.mapper.PlatformOverviewMapper;
import com.yuandian.middle.platform.service.IPlatformOverviewService;
import com.yuandian.utils.PageDataInfo;
import com.yuandian.utils.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lijunhang
 * @date 2023年05月24日 10:32
 * @description 概览及概览详情接口service
 */
@Service
public class PlatformOverviewServiceImpl implements IPlatformOverviewService {

	@Autowired
	private PlatformOverviewMapper platformOverviewMapper;

	/**
	 * 全部或者指定部门类型告警数查询
	 * 部门类型或者公司名称，告警数
	 * 查询告警明细表，告警标识为1的数据
	 * @param param
	 * @return
	 */
	@Override
	public R orgTypeAlarmStatistics(OverViewParam param) {
		if (StringUtils.isNotBlank(param.getOrgType())){
			List<OrgTypeAlarmVo> list=platformOverviewMapper.specialOrgTypeAlarmStatistics(param);
			if(list!=null&&list.size()>5){

				List<OrgTypeAlarmVo> heads=list.subList(0,4);
				List<OrgTypeAlarmVo> overs=list.subList(4,list.size());

				OrgTypeAlarmVo vo=new OrgTypeAlarmVo();
				vo.setOrgTypeIdOrOrgId("");
				vo.setOrgTypeNameOrOrgName("其他");
				vo.setTotalAlarmCount(list.get(0).getTotalAlarmCount());

				overs.forEach(orgTypeAlarmVo->{
					vo.setAlarmCount(vo.getAlarmCount()+orgTypeAlarmVo.getAlarmCount());
				});
				heads.add(vo);
				return R.ok(heads);
			}
			return R.ok(list);
		}else{
			return R.ok(platformOverviewMapper.orgTypeAlarmStatistics(param));
		}
	}

	/**
	 * 全部单位异常能力数查询
	 * @param param
	 * @return
	 */
	@Override
	public R orgTypeAbnormalAbility(OverViewParam param) {
		if (StringUtils.isNotBlank(param.getOrgType())){
			List<OrgTypeAbnormalAbilityVo> list=platformOverviewMapper.specialOrgTypeAbnormalAbility(param);
			if(list!=null&&list.size()>5){

				List<OrgTypeAbnormalAbilityVo> heads=list.subList(0,4);
				List<OrgTypeAbnormalAbilityVo> overs=list.subList(4,list.size());

				OrgTypeAbnormalAbilityVo vo=new OrgTypeAbnormalAbilityVo();
				vo.setOrgTypeIdOrOrgId("");
				vo.setOrgTypeNameOrOrgName("其他");

				overs.forEach(orgTypeAbnormalAbilityVo->{
					vo.setAbnormalAbilityCount(vo.getAbnormalAbilityCount()+orgTypeAbnormalAbilityVo.getAbnormalAbilityCount());
					vo.setTotalAbilityCount(vo.getTotalAbilityCount()+orgTypeAbnormalAbilityVo.getTotalAbilityCount());
					vo.setAlarmCount(vo.getAlarmCount()+orgTypeAbnormalAbilityVo.getAlarmCount());
				});
				heads.add(vo);
				return R.ok(heads);
			}
			return R.ok(list);
		}else{

			return R.ok(platformOverviewMapper.orgTypeAbnormalAbility(param));
		}

	}

	@Override
	public R overviewDetailList(OverViewParam param) {
		if (param.getPageNum() <= 0) {
			param.setPageNum(1);
		}
		if (param.getPageSize() <= 0) {
			param.setPageSize(10);
		}
		IPage<OrgOverviewDetailVo> page = new Page<>(param.getPageNum(), param.getPageSize());
		List<OrgOverviewDetailVo> list =  platformOverviewMapper.overviewDetailList(page, param);
		return R.ok(new PageDataInfo(page.getTotal(), list));
	}
}
